/**
 * @program: LeetCode
 * @description: LeetCode : 53. 最大子数组和
 * @author: WXY
 * @create: 2023-01-29 17:33
 * @Version 1.0
 **/
public class Num053_maxSubArray {
    public static int maxSubArray(int[] nums) {
        if (nums.length < 1) return 0;
        //dp[i]：包括下标i（以nums[i]为结尾）的最大连续子序列和为dp[i]。
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        int max = dp[0];
        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
            max = Math.max(max, dp[i]);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] arr = {-2, 1};
        System.out.println(maxSubArray(arr));

    }
}
